package util

import (
	"common/libutil"
	"common/logging"
	"encoding/json"
	"flag"
)

type Configure struct {
	Log struct {
		File   string
		Level  string
		Name   string
		Suffix string
	}

	Prog struct {
		CPU        int
		Daemon     bool
		HealthPort string
	}

	Server struct {
		Couchbase              string
		Redis                  string
		Mysql                  string
		ServerPort             string
		ServerId               string
		RouteServerAddr        string
		ThriftClientTimeout    int
		KafkaBrokers           []string
		UpstreamTopicBroadcast string
		UpstreamTopicDispatch  string
	}

	Msmart struct {
		MsmartMysql      string
		B2BMysql         string
		AppId            string
		ThriftRpcTimeout uint
		BaseAddr         string
	}

	ApnsPush struct {
		KafkaBrokers []string
		PushType     string
		MsgKey       string
		AppId        int32
		Expiration   int64
	}
	Update struct {
		UpdateRpcAddr    string
		ThriftRpcTimeout uint
	}
	UserInfo struct {
		UserInfoRpcAddr  string
		ThriftRpcTimeout uint
		RpcTopic         string
	}

	KafkaRequestTimeout int

	LockPasscodeExpireTime int

	Ys struct {
		OpenPlatformUrl    string
		AppKey             string
		SecretKey          string
		ProtocolVer        string
		TimeOut            int
		OpenPlatformCAFile string
		MyCAFile           string
		MyKeyFile          string
		LappDeviceVideoUrl string
	}

	DomainName struct {
		Domains []string
	}

	DefaultScene struct {
		Infos []json.RawMessage
	}
}

func NewConfigure(path string) *Configure {
	config := flag.String("a", path, "config file")
	flag.Parse()
	Cfg := &Configure{}
	err := libutil.ParseJSON(*config, &Cfg)
	if err != nil {
		logging.Error("parse config %s error: %s\n", *config, err.Error())
		return nil
	}
	return Cfg
}

var Cfg *Configure

func InitConfigure(file string) bool {
	Cfg = NewConfigure(file)
	if Cfg != nil {
		return true
	}
	return false
}
